package cn.com.xinjinfu.pdoulcProduct.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.com.xinjinfu.pdoulcProduct.R;

/**
 * 底部导航栏
 * @author dewyze
 * 
 */
public class MyTabWidget extends LinearLayout {

	private static final String TAG = "MyTabWidget";
	private List<CheckedTextView> mCheckedList = new ArrayList<CheckedTextView>();
	private List<View> mViewList = new ArrayList<View>();
	// 存放指示点
	private List<TextView> mIndicateImgs = new ArrayList<TextView>();
	private CharSequence[] mLabels;

	public MyTabWidget(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.TabWidget, defStyle, 0);

		mLabels = a.getTextArray(R.styleable.TabWidget_bottom_labels);

		if (null == mLabels || mLabels.length <= 0) {
			a.recycle();
			return;
		}
		a.recycle();
		init(context);
	}


	public MyTabWidget(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public MyTabWidget(Context context) {
		super(context);
		init(context);
	}


	private void init(final Context context) {
		this.setOrientation(LinearLayout.HORIZONTAL);
//		this.setBackgroundResource(R.drawable.index_bottom_bar);
		this.setBackgroundColor(0xffffffff);
		LayoutInflater inflater = LayoutInflater.from(context);
		LayoutParams params = new LayoutParams(
				0, LayoutParams.MATCH_PARENT);
		params.weight = 1.0f;
		params.gravity = Gravity.CENTER;

		int size = mLabels.length;
		for (int i = 0; i < size; i++) {

			final int index = i;

			final View view = inflater.inflate(R.layout.tab_item, null);

			final CheckedTextView itemName = (CheckedTextView) view
					.findViewById(R.id.item_name);

			itemName.setText(mLabels[i]);

			// 指示点ImageView，如有版本更新需要显示
			final TextView indicateImg = (TextView) view
					.findViewById(R.id.indicate_img);

			this.addView(view, params);

			itemName.setTag(index);

			mCheckedList.add(itemName);

			// 将指示图片加到list，便于控制显示隐藏
			mIndicateImgs.add(indicateImg);

			mViewList.add(view);

			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					setTabsDisplay(index);

					if (null != mTabListener) {
						mTabListener.onTabSelected(index);
					}
				}
			});

			if (i == 0) {
				itemName.setChecked(true);
				itemName.setTextColor(Color.rgb(247, 127, 150));
//				view.setBackgroundColor(Color.rgb(250, 250, 250));
			} else {
				itemName.setChecked(false);
				itemName.setTextColor(Color.rgb(102, 102, 102));
//				view.setBackgroundColor(Color.rgb(250, 250, 250));
			}

		}
	}

	/**
	 * 设置指示点的显示
	 *
	 * @param context
	 * @param position
	 *            显示位置
	 * @param visible
	 *            是否显示，如果false，则都不显示
	 */
	public void setIndicateDisplay(Context context, int position,
								   boolean visible) {
		int size = mIndicateImgs.size();
		if (size <= position) {
			return;
		}
		TextView indicateImg = mIndicateImgs.get(position);
		indicateImg.setVisibility(visible ? View.VISIBLE : View.GONE);
	}

	public void setTabsDisplay(int index) {
		int size = mCheckedList.size();
		for (int i = 0; i < size; i++) {
			CheckedTextView checkedTextView = mCheckedList.get(i);
			if ((Integer) (checkedTextView.getTag()) == index) {
				Log.i(TAG, mLabels[index] + " is selected...");
				checkedTextView.setChecked(true);
				checkedTextView.setTextColor(Color.rgb(247, 127, 150));
//				mViewList.get(i).setBackgroundColor(Color.rgb(240, 241, 242));
			} else {
				checkedTextView.setChecked(false);
				checkedTextView.setTextColor(Color.rgb(102, 102, 102));
//				mViewList.get(i).setBackgroundColor(Color.rgb(250, 250, 250));
			}
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);

		int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

		if (widthSpecMode != MeasureSpec.EXACTLY) {
			widthSpecSize = 0;
		}

		if (heightSpecMode != MeasureSpec.EXACTLY) {
			heightSpecSize = 0;
		}

		if (widthSpecMode == MeasureSpec.UNSPECIFIED
				|| heightSpecMode == MeasureSpec.UNSPECIFIED) {
		}

		int width;
		int height;
		width = Math.max(getMeasuredWidth(), widthSpecSize);
		height = Math.max(this.getBackground().getIntrinsicHeight(),
				heightSpecSize);
		setMeasuredDimension(width, height);
	}

	private OnTabSelectedListener mTabListener;

	public interface OnTabSelectedListener {
		void onTabSelected(int index);
	}

	public void setOnTabSelectedListener(OnTabSelectedListener listener) {
		this.mTabListener = listener;
	}

}
